﻿using Caltech.Extensions;
using Caltech.Models;
using Caltech.Service.Common;
using CaltechSoft.Service.EntityFW.Entity;
using CaltechSoft.Service.Repositories;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace Caltech.Controllers
{
    [HasAccess(Areas.ProcedureView)]
    public class ProcedureController : BaseApiController<Procedure, ProcedureModel, ProcedureModel>
    {

        public ProcedureController(IRepository<Procedure> repository)
            : base(repository)
        {
        }

        public override IHttpActionResult Get()
        {
            var queryItems = Request.RequestUri.ParseQueryString();
            string procedureName = queryItems["name"];

            Expression<Func<Procedure, bool>> filter = null;

            if (!string.IsNullOrEmpty(procedureName))
            {
                filter = procedure => procedure.ProcedureName.ToLower().Contains(procedureName.Trim().ToLower());
            }

            return QueryData(filter);
        }

        [HasAccess(Areas.ProcedureCreate)]
        public override async Task<IHttpActionResult> Post(ProcedureModel model)
        {
            return await base.Post(model);
        }

        [HasAccess(Areas.ProcedureEdit)]
        public override async Task<IHttpActionResult> Put(ProcedureModel model)
        {
            return await base.Put(model);
        }

        [HasAccess(Areas.ProcedureDelete)]
        public override async Task<IHttpActionResult> Delete(int id)
        {
            return await base.Delete(id);
        }
    }
}